#include <sstream>
#include "uart.h"
#include "m01_spi.h"
#include "protocol_DragonBall.h"
#include "canbus_dragonball.h"
#include "uart9bit.h"
#include "usart_dragonball.h"

			
namespace igslib
{
	ISerialManager* CSerialManager::instance = 0;
	ISerialManager* CSerialManager::GetInstance() 
	{
		if ( instance == 0 )
		{
			instance = new CSerialManager();
		}
		return instance ;
	}
    IUart* CSerialManager::NewM01Uart()
    {
        IUart* rtn = nullptr;
        rtn = new RS232();
        cpns.insert(rtn);
        return rtn;
    }
    ISpi* CSerialManager::NewM01Spi()
    {
        ISpi* rtn = nullptr;
        rtn = new m01_spi();
        cpns.insert(rtn);
		return rtn;
    }
    ISpi* CSerialManager::New_ProjMCU_spi(const std::string& name)
    {
        ISpi* rtn = nullptr;
		if(name=="DragonBallRace")
        {
            rtn = new Protocol_DragonBall();
        }else if(name=="DragonWarrior")
        {
            rtn = new Protocol_DragonBall();
        }
        cpns.insert(rtn);
		return rtn;
    }
    ICanbus* CSerialManager::NewM01Canbus()
    {
        ICanbus* rtn = nullptr;
        rtn = new CCanbus();
        cpns.insert(rtn);
        return rtn;
    }
	ICanbus* CSerialManager::New_ProjMCU_canbus(const std::string& name)
    {
        ICanbus* rtn = nullptr;
		if(name=="DragonBallRace")
		{
			rtn = new canbus_Grgn();
            cpns.insert(rtn);
        }
		return rtn;
    }
    IUart9bit* CSerialManager::NewM01Uart9bit()
    {
        IUart9bit* rtn = nullptr;
        rtn = new RS232_9bit();
        return rtn;
    }
    std::map<std::string,IMCUUart*> CSerialManager::New_ProjMCU_uart(const std::string& name)
	{
		std::map<std::string,IMCUUart*> rtn;
		if(name=="DragonBallRace")
		{
			rtn["CON10"] = new usart_Grgn();		
            cpns.insert(rtn["CON10"]);
			rtn["CON3"] = new usart_Grgn();		
            cpns.insert(rtn["CON3"]);
        }
		return rtn;
	}
		
}

extern "C"{
	igslib::ISerialManager* GetSerialManagerInstance()
	{
        igslib::ISerialManager* rtn = nullptr;
        rtn = igslib::CSerialManager::GetInstance();
		return rtn;
	}
}
